<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>将对象转换为一个query的路径参数</title>
  </head>
  <body>
    <p>转换前</p>
    <div>let objectTest = { a: 500, c: 66, };</div>

    <div><button class="btn">点击转换</button></div>
    <div class="showText"></div>
    <script>
      // 传入一个对象和一个布尔值，如果传入布尔值为false，则不用添加？号
      const queryString = (obj, mark = true) =>
        (mark ? "?" : "") +
        Object.keys(obj)
          .map((key) => `${key}=${encodeURIComponent(obj[key])}`)
          .join("&");
      let objectTest = {
        a: 500,
        c: 66,
      };
      //   let x = queryString(objectTest, false);
      //   console.log(x);

      const showText = document.querySelector(".showText");
      console.log(showText);
      document.querySelector("button").onclick = function () {
        let x = queryString(objectTest, false);
        console.log(x);
        showText.innerHTML = x;
      };
    </script>
  </body>
</html>
